<?php

class Admin_Model_Mapper_Language{
	protected $_dbTable;
 
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Admin_Model_DbTable_Language');
        }
        return $this->_dbTable;
    }
		
	public function getPaginator($page, $itemsPerPage){	
		$paginator = Zend_Paginator::factory($this->getDbTable()->select());
		
		$paginator->setCurrentPageNumber($page);
		
		$paginator->setItemCountPerPage($itemsPerPage);

		$paginator->setPageRange(5);
		
		return $paginator; 
	}
	
	public function getPaginatorData($page, $itemsPerPage){
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->order("language_id desc")->limit($itemsPerPage, ($page-1) * $itemsPerPage));
		
		return $this->CreateResultSetEntity($resultSet);
	}
	
	public function find($languageId, Admin_Model_Language $language){
		$result = $this->getDbTable()->find($languageId); 
		if (0 == count($result)){
			return;
		}		
		
		$row = $result->current();
		
        $language->setLanguageId($row->language_id)
                  ->setLanguageName($row->language_name)
                  ->setLanguageIsActive($row->language_isactive)
				  ->setLanguageIsShowInSourceList($row->language_isshow_insourcelist)
				  ->setLanguageIsShowInTargetList($row->language_isshow_intargetlist)
				  ->setLanguageIsShowInTranslatorlist($row->language_isshow_intranslatorlist);		
	}
	
	public function getAllActiveLanguage(){		
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where("language_isactive = 'Y'")->order("language_name asc"));
    
		return $this->CreateResultSetEntity($resultSet);
	}
	
	public function getSourceLanguageActive(){
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where(" (language_isactive = 'Y') and (language_isshow_insourcelist='Y')")->order("language_name asc"));
    
		return $this->CreateResultSetEntity($resultSet);
	}
	
	public function getTargetLanguageActive(){
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where("(language_isactive = 'Y') and (language_isshow_intargetlist='Y')")->order("language_name asc"));
    
		return $this->CreateResultSetEntity($resultSet);
	}
	
	public function getTranslatorlistActive(){
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where("(language_isactive = 'Y') and (language_isshow_intranslatorlist='Y')")->order("language_name asc"));
    
		return $this->CreateResultSetEntity($resultSet);
	}
		
	public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        
		return $this->CreateResultSetEntity($resultSet);
    }

	public function save(Admin_Model_Language $language){			
		$data = array(
            'language_id'   => $language->getLanguageId(),
            'language_name' => $language->getLanguageName(),
            'language_isactive' => $language->getLanguageIsActive(),
			'language_isshow_insourcelist' => $language->getLanguageIsShowInSourceList(),
			'language_isshow_intargetlist' => $language->getLanguageIsShowInTargetList(),
			'language_isshow_intranslatorlist' => $language->getLanguageIsShowInTranslatorlist()
        );

        if (null === ($languageId = $language->getLanguageId())) {
            unset($data['language_id']);
            return $this->getDbTable()->insert($data);
        }

        return   $this->getDbTable()->update($data, array('language_id = ?' => $languageId));
	}
	
	
	public function deleteLanguage($languageId){		
		$languages = $this->getDbTable()->find((int)$languageId);
		$languages[0]->delete();
	}
	
	public function getLanguageByLanguageName($languageName, $languageId){
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->where("(language_name ='". $languageName . "') and (language_id <> " . $languageId . " ) ")->order("language_id desc"));
    
		return $this->CreateResultSetEntity($resultSet);
	}
	
	private function CreateResultSetEntity($resultSet){
		$entries   = array();
		
		foreach ($resultSet as $row) {
			$entry = new Admin_Model_Language();
			$entry->setLanguageID($row->language_id)
				  ->setLanguageName($row->language_name)
				  ->setLanguageIsActive($row->language_isactive)
				  ->setLanguageIsShowInSourceList($row->language_isshow_insourcelist)
				  ->setLanguageIsShowInTargetList($row->language_isshow_intargetlist)
				  ->setLanguageIsShowInTranslatorlist($row->language_isshow_intranslatorlist);
				  
			$entries[] = $entry;
		}
		
		return $entries;
	}
}